var goldOrderId = 48;
// 刷新级别
var updateLevelMap = {
    'AUTO' : 0,     // 自动刷新
    'NORMAL' : 1,   // 普通刷新
    'SILVER' : 2,   // 白金刷新
    'SUPER' : 3,    // 至尊刷新
};

exports.get = function(player, req, resp, onHandled) {
    do {
        var updateLevel =  parseInt(req.args.level);
        if( isNaN(updateLevel) || updateLevel < 0 || updateLevel > 3 ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        var userLevel = user.status.level;
        if( userLevel < gConfGlobal.BlackShopLevelLimit ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        var blackShop = user.black_shop; 
        var lastUpdateTime = blackShop.timer;
        var now = common.getTime();
        // 更新时间未到时 
        if( (updateLevelMap.AUTO == updateLevel) && ((now - lastUpdateTime) < (gConfGlobal.BlackShopUpdateTime - 20)) ) {
            resp.data.black_shop = blackShop;
            break; 
        }
        
        var costNum = 0;
        if( updateLevelMap.AUTO == updateLevel ) {
            blackShop.timer = now;
            player.markDirty('black_shop.timer');
            updateLevel = 1;
        }else {
            if( updateLevelMap.NORMAL == updateLevel ) {
               costNum = gConfGlobal.BlackShopNormalGoldOrder;   
            }else if( updateLevelMap.SILVER == updateLevel ) {
               costNum = gConfGlobal.BlackShopSilverGoldOrder;   
            }else if( updateLevelMap.SUPER == updateLevel ) {
               costNum = gConfGlobal.BlackShopSuperGoldOrder;   
            }

            var ownNum = player.getInventory('material', goldOrderId); 
            if( ownNum < costNum ) {
                resp.code = 1; resp.desc = 'not enough gold order'; break;
            }
            
            player.addInventory('material', goldOrderId, -costNum)
        }
        
        if( updateLevelMap.AUTO == updateLevel ) {
            // 自动刷新时的权重和普通刷新为同一个级别 
            updateLevel = update.NORMAL;
        }

        // 选出符合玩家君主等级的商品
        var weights = {}; 
        for( var id in gConfBlackShop ) {
            var blackShopConf = gConfBlackShop[id];
            var limitSell = blackShop.limit_sell;
            if( limitSell[id] && (limitSell[id] >= blackShopConf.BuyLimit) ) {
                continue; 
            }

            if( (userLevel < blackShopConf.MinLevel) || (userLevel > blackShopConf.MaxLevel) ) {
                continue; 
            }

            weights[id] = blackShopConf['Weight'+updateLevel] 
        }

        var sell = {};
        for( var i = 1; i <= 3; i++ ) {
            var id = +common.wRand(weights);
            if( !id ) {
                continue; 
            }
            
            sell[id]  = 0;
            delete weights[id]; 
        }

        blackShop.sell = sell;
        player.markDirty('black_shop.sell');

        resp.data.black_shop = blackShop;
        resp.data.order = -costNum;
        
    }while(false);

    onHandled();
}

exports.buy = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var id = +req.args.id;

        var user = player.user;
        var sell = user.black_shop.sell;
        var limitSell = user.black_shop.limit_sell;

        var blackShopConf = gConfBlackShop[id];
        if( !blackShopConf || !(id in sell) || sell[id] ) {
            resp.code = 1; resp.desc = 'id error'; break;
        }

        var costType = blackShopConf.Currency;
        var costNum = blackShopConf.Price;
        
        if( !player.costResource(costType, costNum, id) ) {
            resp.code = 1; resp.desc = 'not enough currency'; break;
        }
        
        var awards = [];    // 购买结果,用award格式返回
        var award = blackShopConf.Award1;
        if( award ) {
            awards.push(award);
            awards = player.addAwards(awards); 

            if( blackShopConf.Message && (common.getDayPassed(gConfGlobalServer.ServerStartTime) <= 14) 
                && (award[0] == 'material' || award[0] == 'gem') ) {
                var itemName = '';
                if( award[0] == 'material' ) {
                    itemName = gConfMaterial[award[1]].Name;
                }else{
                    itemName = gConfGem[award[1]].Name;
                }
                gMessage.addSystemMessage('black_shop', [user.info.un, award[2], itemName]);
            }
        }

        sell[id] = 1;
        player.markDirty(util.format('black_shop.sell.%d', id));

        if( blackShopConf.BuyLimit ) {
            if( !(id in limitSell) ) {
                limitSell[id] = 0; 
            }

            limitSell[id] += 1; 
            player.markDirty(util.format('black_shop.limit_sell.%d', id));
        }

        resp.data.cost_type = costType;
        resp.data.cost_num = -costNum;
        resp.data.awards = awards;

    }while(false);

    onHandled();
}
